<cfparam name="userIDs" type="string" default="0">
<cfparam name="organizationIDs" type="string" default="0">
<cfparam name="workshopIDs" type="string" default="0">
<cfparam name="studentIDs" type="string" default="0">
<cfparam name="orderIDs" type="string" default="0">
<cfparam name="fundIDs" type="string" default="0">
<cfparam name="scholarshipIDs" type="string" default="0">
<cfparam name="recipientsWDDX" type="string" default="">

<cfif NOT IsWDDX(recipientsWDDX)>

	<cfif userIDs neq 0>
		<cfset uIDs = "">
		<cfloop index="uID" list="#userIDs#">
			<cfif NOT ListFind(uIDs, uID)>
				<cfset uIDs = ListAppend(uIDs, uID)>
			</cfif>
		</cfloop>
		<cfset recipients = application.userGateway.getAllAsQuery(orderBy = "Organization, Last_Name", orderDir = "asc", userIDs = uIDs)>
	<cfelseif organizationIDs neq 0>
		<cfset organizationUsers = application.userGateway.getAllAsQuery(fieldlist = "User_ID", organizationIDs = organizationIDs)>
		<cfset uIDs = "">
		<cfloop index="uID" list="#ValueList(organizationUsers.User_ID)#">
			<cfif NOT ListFind(uIDs, uID)>
				<cfset uIDs = ListAppend(uIDs, uID)>
			</cfif>
		</cfloop>
		<cfset recipients = application.userGateway.getAllAsQuery(orderBy = "Organization, Last_Name", orderDir = "asc", userIDs = uIDs)>
	<cfelseif workshopIDs neq 0>
		<cfset workshops = application.workshopGateway.getAllAsQuery(fieldlist = "Organizer_ID, Manager_ID", workshopIDs = workshopIDs)>
		<cfset uIDs = "">
		<cfloop query="workshops">
			<cfif NOT ListFind(uIDs, Organizer_ID)>
				<cfset uIDs = ListAppend(uIDs, Organizer_ID)>
			</cfif>
			<cfif NOT ListFind(uIDs, Manager_ID)>
				<cfset uIDs = ListAppend(uIDs, Manager_ID)>
			</cfif>
		</cfloop>
		<cfset recipients = application.userGateway.getAllAsQuery(orderBy = "Organization, Last_Name", orderDir = "asc", userIDs = uIDs)>
	<cfelseif fundIDs neq 0>
		<!--- Goes to the organizer who requested the funds --->
		<cfset funds = application.fundGateway.getAllAsQuery(fundIDs = fundIDs)>
		<cfset uIDs = "">
		<cfloop query="funds">
			<cfif NOT ListFind(uIDs, Requested_By_ID)>
				<cfset uIDs = ListAppend(uIDs, Requested_By_ID)>
			</cfif>
		</cfloop>
		<cfset recipients = application.userGateway.getAllAsQuery(orderBy = "Organization, Last_Name", orderDir = "asc", userIDs = uIDs)>
	<cfelseif studentIDs neq 0>
		<cfset recipients = application.studentGateway.getAllAsQuery(orderBy = "Last_Name", orderDir = "asc", studentIDs = studentIDs)>
	<cfelseif orderIDs neq 0>
		<cfset orders = application.orderGateway.getAllAsQuery(fieldlist = "Organizer_ID", orderIDs = orderIDs)>
		<cfset uIDs = "">
		<cfloop query="orders">
			<cfif NOT ListFind(uIDs, Organizer_ID)>
				<cfset uIDs = ListAppend(uIDs, Organizer_ID)>
			</cfif>
		</cfloop>
		<cfset recipients = application.userGateway.getAllAsQuery(orderBy = "Organization, Last_Name", orderDir = "asc", userIDs = uIDs)>
	<cfelseif scholarshipIDs neq 0>
		<cfset recipients = application.scholarshipGateway.getAllAsQuery(scholarshipIDs = scholarshipIDs)>
	<cfelseif institutionIDs neq 0>
		Institutions E-mail Selected feature not implemented.<cfabort>
	</cfif>
	<cfset recipientsArray = ArrayNew(1)>
	<cfset invalidRecipientsArray = ArrayNew(1)>
	<cfloop query="recipients">
		<cfset recipientStruct = StructNew()>
		<cfset recipientStruct.firstName = XMLFormat(First_Name, true)>
		<cfset recipientStruct.lastName = XMLFormat(Last_Name, true)>
		<cfset recipientStruct.email = XMLFormat(Email, true)>
		<cfif studentIDs neq 0 or scholarshipIDs neq 0>
			<cfset recipientStruct.claimID = XMLFormat(Claim_ID, true)>
		</cfif>
		<cfif IsDefined("Institution1")>
			<cfset recipientStruct.organization = XMLFormat(Institution1, true)>
			<cfif Len(Trim(Institution2)) gt 0 and Len(Trim(Institution3)) eq 0>
				<cfset recipientStruct.organization = recipientStruct.organization & " and " & XMLFormat(Institution2, true)>
			<cfelseif Len(Trim(Institution2)) gt 0 and Len(Trim(Institution3)) gt 0>
				<cfset recipientStruct.organization = recipientStruct.organization & ", " & XMLFormat(Institution2, true) & " and " & XMLFormat(Institution3, true)>
			</cfif>
		<cfelse>
			<cfset recipientStruct.organization = XMLFormat(Organization, true)>
		</cfif>
		<cfif IsValid("email", recipientStruct.email)>
			<cfset ArrayAppend(recipientsArray, recipientStruct)>
		<cfelse>
			<cfset ArrayAppend(invalidRecipientsArray, recipientStruct)>
		</cfif>
	</cfloop>

</cfif>

<cfwddx action="cfml2wddx" input="#recipientsArray#" output="recipientsWDDX">

<cfoutput>

	<cfif recipients.RecordCount gt 0>

		<script type="text/javascript" language="javascript">
		<!--
			toggleRecipientsContainer = function() {
				if (document.getElementById("recipientsContainer").style.visibility == "visible") {
					document.getElementById("recipientsContainer").style.visibility = "hidden";
					document.getElementById("recipientsContainer").style.display = "none";
					document.getElementById("showRecipientsLink").innerHTML = "Show recipients";
				} else {
					document.getElementById("recipientsContainer").style.visibility = "visible";
					document.getElementById("recipientsContainer").style.display = "block";
					document.getElementById("showRecipientsLink").innerHTML = "Hide recipients";
				}
			}
		// -->
		</script>

		<cfform name="emailForm" id="emailForm" action="#script_name#" method="post">
			<cfinput type="hidden" name="recipientsWDDX" value="#xmlFormat(recipientsWDDX)#">
			<!--- <cfinput type="hidden" name="m" value="mailChimpListSave">
			<cfinput type="hidden" name="view" value="mailChimpLists"> --->
			<!--- <cfinput type="hidden" name="format" value="HTML"> --->

			<div class="buttonBar">
				<a href="" onclick="history.go(-1); return false;" class="formLink2">Back</a>
				<cfinput type="submit" name="nextButton" id="nextButton" value="Next" class="formButton">
			</div>
			
			<p>#ArrayLen(recipientsArray)# valid recipient(s)<cfif ArrayLen(invalidRecipientsArray) gt 0>, #ArrayLen(invalidRecipientsArray)# invalid recipients</cfif>&nbsp;&nbsp;[<a href="" id="showRecipientsLink" onclick="toggleRecipientsContainer(); return false;">Show recipients</a>]</p>
			<div id="recipientsContainer" style="visibility: hidden; display: none; padding: 6px;">
				<table class="list" style="width: 100%;">
					<tr>
						<th><strong>Name</strong></th>
						<th><strong>E-mail</strong></th>
						<th><strong>Organization</strong></th>
					</tr>
					<cfif ArrayLen(invalidRecipientsArray) gt 0>
						<tr>
							<td colspan="3" style="background-color: white; color: navy; border-bottom: solid 1px navy;"><strong>Invalid Recipients</strong></td>
						</tr>
						<cfloop index="r" from="1" to="#ArrayLen(invalidRecipientsArray)#">
							<tr<cfif r mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
								<td>#invalidRecipientsArray[r].firstName# #invalidRecipientsArray[r].lastName#&nbsp;</td>
								<td>#invalidRecipientsArray[r].email#&nbsp;</td>
								<td>#invalidRecipientsArray[r].organization#&nbsp;</td>
							</tr>
						</cfloop>
						<tr>
							<td colspan="3" style="background-color: white; color: navy; border-bottom: solid 1px navy;"><strong>Valid Recipients</strong></td>
						</tr>
					</cfif>
					<cfloop index="r" from="1" to="#ArrayLen(recipientsArray)#">
						<tr<cfif r mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
							<td>#recipientsArray[r].firstName# #recipientsArray[r].lastName#&nbsp;</td>
							<td>#recipientsArray[r].email#&nbsp;</td>
							<td>#recipientsArray[r].organization#&nbsp;</td>
						</tr>
					</cfloop>
				</table>
			</div>

			<table class="form">
				<tr>
					<td colspan="2" class="formHeader">Create New List</td>
				</tr>
				<tr>
					<th>List Name:</th>
					<td><cfinput type="text" name="listName" value="" required="yes" message="List Name is required" maxlength="200" size="36"></td>
				</tr>				
				<tr>
					<th>Recipients:</th>
					<td style="line-height: 16px;">
						#ArrayLen(recipientsArray)# valid recipient(s)<cfif ArrayLen(invalidRecipientsArray) gt 0>, #ArrayLen(invalidRecipientsArray)# invalid recipients</cfif>&nbsp;&nbsp;[<a href="" id="showRecipientsLink" onclick="toggleRecipientsContainer(); return false;">Show recipients</a>]
						<div id="recipientsContainer" style="visibility: hidden; display: none; padding: 6px;">
							<table class="list" style="width: 100%;">
								<tr>
									<th><strong>Name</strong></th>
									<th><strong>E-mail</strong></th>
									<th><strong>Organization</strong></th>
								</tr>
								<cfif ArrayLen(invalidRecipientsArray) gt 0>
									<tr>
										<td colspan="3" style="background-color: white; color: navy; border-bottom: solid 1px navy;"><strong>Invalid Recipients</strong></td>
									</tr>
									<cfloop index="r" from="1" to="#ArrayLen(invalidRecipientsArray)#">
										<tr<cfif r mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
											<td>#invalidRecipientsArray[r].firstName# #invalidRecipientsArray[r].lastName#&nbsp;</td>
											<td>#invalidRecipientsArray[r].email#&nbsp;</td>
											<td>#invalidRecipientsArray[r].organization#&nbsp;</td>
										</tr>
									</cfloop>
									<tr>
										<td colspan="3" style="background-color: white; color: navy; border-bottom: solid 1px navy;"><strong>Valid Recipients</strong></td>
									</tr>
								</cfif>
								<cfloop index="r" from="1" to="#ArrayLen(recipientsArray)#">
									<tr<cfif r mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
										<td>#recipientsArray[r].firstName# #recipientsArray[r].lastName#&nbsp;</td>
										<td>#recipientsArray[r].email#&nbsp;</td>
										<td>#recipientsArray[r].organization#&nbsp;</td>
									</tr>
								</cfloop>
							</table>
						</div>
					</td>
				</tr>
				<tr>
					<th>From:</th>
					<td>
						info@calgrants.org
						<!--- <cfselect name="from" required="yes" message="From is required">
							<option value="#application.adminEmail#">#application.adminEmail#</option>
							<option value="#authUser.getEmail()#">#authUser.getEmail()#</option>
						</cfselect> --->
					</td>
				</tr>
				<tr>
					<th nowrap="nowrap">Dynamic Variables:</th>
					<td>
						%firstName%, %lastName%,<cfif studentIDs neq 0 or scholarshipIDs neq 0> %claimID%,</cfif> %email% and %organization%
					</td>
				</tr>
				<tr>
					<th>Subject:</th>
					<td><cfinput type="text" name="subject" value="" required="yes" message="Subject is required" maxlength="200" style="width: 700px;"></td>
				</tr>
				<tr>
					<th nowrap="nowrap">Message:<!--- <br>
					<br>
					%firstName%<br>
					%lastName%<br>
					%organization%<br>
					%email%<br> --->
					</th>
					<td><cftextarea name="message" required="yes" style="height: 400px; width: 700px;" message="Message is required" richtext="yes" basepath="/JelCMS_Editor/" toolbar="Basic"></cftextarea></td>
				</tr>
			</table>

		</cfform>
		
		<!--- <cfset mailChimp = createObject("component","globals.cfc.mailChimp2").init(application.MAILCHIMP_API_KEY,application.MAILCHIMP_SERVICE_URL,application.MAILCHIMP_OUTPUT_FORMAT,application.MAILCHIMP_USERNAME,application.MAILCHIMP_PASSWORD)>
		
		<cfset rLogin = mailChimp.login()>
		<cfdump var="#rLogin#">
		
		<cfset lists = mailChimp.lists()>
		
		<h2>MailChimp Lists</h2>
		
		<div class="h2Content">
		
			<p>Select the list you would like to use for these recipients.  Please note that all subscribers will be removed and replaced with the recipients above.</p>
			
			<table class="list" width="100%">
				<tr>
					<th>List</th>
					<th>Members</th>
					<th>Date Created</th>
				</tr>
				<cfloop query="lists">
					<tr<cfif currentRow mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
						<td>#name#</td>
						<td>#membercount#</td>
						<td>#DateFormat(createdon, "mmmm d, yyyy")# #TimeFormat(createdon, "h:mmtt")#</td>
					</tr>
				</cfloop>
			</table>
		
		</div> --->

	<cfelse>

		<p>(No recipients)</p>

	</cfif>

</cfoutput>